/*
 * @(#)TraditionalServer.java    Jul 23, 2010
 *
 * Copyright 2009 Nyber Shanghai, Inc. All rights reserved.
 */

package com.lab.net;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Class description goes here.
 *
 * @author nickevin@gmail.com
 * @version Jul 23, 2010 9:58:33 AM
 */

public class TraditionalServer {

	public static void main(String args[]) {

		int port = 2000;
		ServerSocket server;
		DataInputStream input;

		try {

			server = new ServerSocket(port);
			System.out.println("Server waiting for client on port "
					+ server.getLocalPort());

			// server infinite loop
			while (true) {
				Socket client = server.accept();
				System.out.println("New connection accepted "
						+ client.getInetAddress() + ":" + client.getPort());
				input = new DataInputStream(client.getInputStream());
				// print received data 
				try {
					byte[] byteArray = new byte[4096];
					while (true) {
						int nread = input.read(byteArray, 0, 4096);
						if (0 == nread) {
							break;
						}
					}
				} catch (IOException e) {
					System.out.println(e);
				}

				// connection closed by client
				try {
					client.close();
					System.out.println("Connection closed by client");
				} catch (IOException e) {
					System.out.println(e);
				}

			}

		}

		catch (IOException e) {
			System.out.println(e);
		}
	}
}
